package org.fujay.langchain4jserver.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.fujay.langchain4jserver.domain.model.aggregate.Model;
import org.fujay.langchain4jserver.domain.model.repository.ModelRepository;
import org.fujay.langchain4jserver.domain.model.vo.ModelId;
import org.fujay.langchain4jserver.domain.model.vo.ModelType;
import org.fujay.langchain4jserver.infrastructure.persistence.assembler.ModelAssembler;
import org.fujay.langchain4jserver.infrastructure.persistence.mybatis.entity.ModelPo;
import org.fujay.langchain4jserver.infrastructure.persistence.mybatis.mapper.ModelMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 模型仓储实现
 */
@Repository
@RequiredArgsConstructor
public class ModelRepositoryImpl implements ModelRepository {

    private final ModelMapper modelMapper;

    @Override
    public boolean existsById(ModelId id) {
        if (id == null || id.value() == null) {
            return false;
        }
        return modelMapper.selectCount(new LambdaQueryWrapper<ModelPo>().eq(ModelPo::getId, id.value()))
                > 0;
    }

    @Override
    public Optional<Model> findById(ModelId id) {
        if (id == null || id.value() == null) {
            return Optional.empty();
        }
        ModelPo modelPO = modelMapper.selectById(id.value());
        return Optional.ofNullable(modelPO).map(ModelAssembler::toAggregate);
    }

    @Override
    public List<Model> findAll() {
        List<ModelPo> modelPOs = modelMapper.selectList(null); // 查询所有
        return modelPOs.stream()
                .map(ModelAssembler::toAggregate) // 转换为领域模型
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void save(Model model) {
        if (model == null) {
            return;
        }
        ModelPo modelPO = ModelAssembler.toPO(model);

        if (modelMapper.selectById(model.getId().value()) == null) {
            modelMapper.insert(modelPO);
        } else {
            modelMapper.updateById(modelPO);
        }
    }

    @Override
    @Transactional
    public void deleteById(ModelId id) {
        if (id != null && id.value() != null) {
            modelMapper.deleteById(id.value());
        }
    }

    @Override
    public List<Model> findEnabledByType(ModelType modelType) {
        if (modelType == null) {
            return List.of();
        }
        List<ModelPo> modelPOs =
                modelMapper.selectList(
                        new LambdaQueryWrapper<ModelPo>()
                                .eq(ModelPo::getModelType, modelType.name()) // 存储枚举名称
                                .eq(ModelPo::isEnabled, true)); // 使用 isEnabled() 对应 boolean enabled
        return modelPOs.stream().map(ModelAssembler::toAggregate).collect(Collectors.toList());
    }

    @Override
    public List<Model> findBySupplier(String supplierCode) {
        if (supplierCode == null || supplierCode.isBlank()) {
            return List.of();
        }
        // 使用正确的字段名 getModelSupplier
        List<ModelPo> modelPOs =
                modelMapper.selectList(
                        new LambdaQueryWrapper<ModelPo>().eq(ModelPo::getModelSupplier, supplierCode));
        return modelPOs.stream().map(ModelAssembler::toAggregate).collect(Collectors.toList());
    }
}
